package cc.orange.mainView;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.RecyclerView;
import cc.orange.databinding.FragmentDiscover1Binding;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
//import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.R;
import cc.orange.adapter.DiscoverAdapter;
import cc.orange.base.BaseFragment;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.PersonContentEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.emojis.RongUtils;
import cc.orange.utils.popup.PopupDiscoView;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;
import cc.orange.utils.ZXToastUtil;

public class Discover1Fragment extends BaseFragment {

    private FragmentDiscover1Binding binding;
    private DiscoverAdapter adapter;
    private List<PersonContentEntity.Data> list = new ArrayList<>();
    private  ZXSharedPrefUtil sharedPrefUtil ;
    private int type;//从何处进入
    private String uid;//从何处进入

    public Discover1Fragment(int type, String uid){
        this.type = type;
        this.uid = uid;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_discover1,container,false);
        sharedPrefUtil = new ZXSharedPrefUtil();
        initViews();
        if(type == 1){//发现  动态
            dynamicAll();
        }else{//个人中心动态
            binding.strollFrame.setPadding(0,0,0, RongUtils.dip2px(90));
            returnDongTai(uid);
        }
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getStatus(String str){
        if(str.contains("pub_refres")){
            if(type == 1){//发现  动态
                list.clear();
                adapter.notifyDataSetChanged();
                dynamicAll();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initViews() {
        binding.strollFrame.setLayoutManager(new LinearLayoutManager(getActivity()));

        //下拉刷新
        binding.smartRL.setRefreshHeader(new ClassicsHeader(getActivity()));
        binding.smartRL.setRefreshFooter(new ClassicsFooter(getActivity()));
        binding.smartRL.setEnableLoadMore(true);
        binding.smartRL.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                if(type == 1){//发现  动态
                    dynamicAll();
                }else{//个人中心动态
                    returnDongTai(uid);
                }
                binding.smartRL.finishRefresh(3);
            }
        });
        binding.smartRL.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                binding.smartRL.finishLoadMore(3);
            }
        });

        adapter = new DiscoverAdapter(list);
        adapter.setType(type);
        binding.strollFrame.setAdapter(adapter);

        adapter.setOnItemsListeners(new DiscoverAdapter.onItemsListener() {
            @Override
            public void itemsListener1(int position) {//点击更多
                boolean isLogin_talk = sharedPrefUtil.getBool("isLogin_talk", false);
                if(isLogin_talk) {
                    showPopu("" + list.get(position).getWomanId());
                }else{
                    ZXToastUtil.showToast("请先登录");
                }
            }

            @Override
            public void itemsListener2(int position) {//点击私信
                boolean isLogin_talk = sharedPrefUtil.getBool("isLogin_talk", false);
                if(isLogin_talk) {
                    Intent intent = new Intent(getActivity(), ConversationListActivity.class);
//                    if(0 != list.get(position).getWomanId()){
                        intent.putExtra("uid",""+list.get(position).getWomanId());
//                    }else {
//                        intent.putExtra("uid",""+list.get(position).getUserId());
//                    }
                    intent.putExtra("name",""+list.get(position).getName());
                    startActivity(intent);
                }else{
                    ZXToastUtil.showToast("请先登录");
                }
            }
            @Override
            public void itemsListener3(int position) {//点击头像
                boolean isLogin_talk = sharedPrefUtil.getBool("isLogin_talk", false);
                if(isLogin_talk) {
                    Intent intent = new Intent(getActivity(), MsgActivity.class);
//                    if(0 != list.get(position).getWomanId()){
                        intent.putExtra("uid",""+list.get(position).getWomanId());
//                    }else {
//                        intent.putExtra("uid",""+list.get(position).getUserId());
//                    }
//                    intent.putExtra("uid",""+list.get(position).getWomanId());
                    startActivity(intent);
                }else{
                    ZXToastUtil.showToast("请先登录");
                }
            }
            @Override
            public void itemsListener4(int position) {//点赞
                boolean isLogin_talk = sharedPrefUtil.getBool("isLogin_talk", false);
                if(isLogin_talk) {
//                    if(0 != list.get(position).getWomanId()){
                        defaultgetLike("" + list.get(position).getId(),
                                "" + list.get(position).getWomanId(),
                                list.get(position).getStatus() == 1 ? 0 : 1, position);
//                    }else {
//                        defaultgetLike("" + list.get(position).getId(),
//                                "" + list.get(position).getUserId(),
//                                list.get(position).getStatus() == 1 ? 0 : 1, position);
//                    }

                }
            }
        });
    }

    private void showPopu(String uid){
        PopupDiscoView.showPopupwindow(getActivity(), binding.smartRL, new PopupDiscoView.PopuStatus() {
            @Override
            public void popupShow() {

            }

            @Override
            public void popupDismissCancle(String se) {
                if(se.equals("1")){
                    noWatch(uid,1);
                }
            }
        });
    }

    // 不看Ta的动态
    private void noWatch(String uid,int flags){
        RetrofitFactory.create(ApiService.class)
                .noWatch(getTokens(),uid,""+flags)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            ZXToastUtil.showToast("设置成功");
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void returnDongTai(String uid){
        RetrofitFactory.create(ApiService.class)
                .returnDongTai(getTokens(),uid)
                .enqueue(new DefaultCallback<PersonContentEntity>() {
                    @Override
                    public void onStart(Call<PersonContentEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<PersonContentEntity> call, HttpError error) {
                        cancelLoading();
                        if(list.size() > 0){
                            binding.llNone.setVisibility(View.GONE);
                        }else{
                            binding.llNone.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onSuccess(Call<PersonContentEntity> call, PersonContentEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.addAll(baseEntity.getData());
                            adapter.notifyDataSetChanged();

                            if(list.size() > 0){
                                binding.llNone.setVisibility(View.GONE);
                            }else{
                                binding.llNone.setVisibility(View.VISIBLE);
                            }
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }


        private void dynamicAll(){
        RetrofitFactory.create(ApiService.class)
                .dynamicAll(getTokens())
                .enqueue(new DefaultCallback<PersonContentEntity>() {
                    @Override
                    public void onStart(Call<PersonContentEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<PersonContentEntity> call, HttpError error) {
                        cancelLoading();
                        if(list.size() > 0){
                            binding.llNone.setVisibility(View.GONE);
                        }else{
                            binding.llNone.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onSuccess(Call<PersonContentEntity> call, PersonContentEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.addAll(baseEntity.getData());
                            adapter.notifyDataSetChanged();

                            if(list.size() > 0){
                                binding.llNone.setVisibility(View.GONE);
                            }else{
                                binding.llNone.setVisibility(View.VISIBLE);
                            }
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void defaultgetLike(String dId,String uId,int flag,int position){//flag为点赞标识 1不点赞 0点赞
        RetrofitFactory.create(ApiService.class)
                .defaultgetLike(getTokens(),dId,uId,""+flag)
                .enqueue(new DefaultCallback<PersonContentEntity>() {
                    @Override
                    public void onStart(Call<PersonContentEntity> call) {
                        showLoading_base(getActivity());
                    }

                    @Override
                    public void onError(Call<PersonContentEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<PersonContentEntity> call, PersonContentEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            list.get(position).setStatus(flag);
                            adapter.notifyDataSetChanged();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }



    @Override
    public void onResume() {
        super.onResume();
//        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
//            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }
}